package com.ascent.client.ui;

import com.ascent.bean.Product;
import com.ascent.client.util.ProductDataClient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * 更新药品信息窗体
 */
public class UpdateProductFrame extends JFrame {
    private JTextField productname;

    private JTextField cas;
    private JTextField structure;
    private JTextField formula;
    private JTextField price;
    private JTextField realstock;
    private JTextField category;

    public String getCategoryText() {
        return categoryText;
    }

    String categoryText;

    private JLabel tip;

    private ProductDataClient productDataClient;

    public UpdateProductFrame(){
        this(new Product());
    }
    UpdateProductFrame(Product product){
        this.setTitle("修改药品信息");
        categoryText = product.getCategory();

        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());

        JPanel addProductPanel = new JPanel();

        JLabel productnameLabel = new JLabel("\u3000药品名称：");
        JLabel casLabel = new         JLabel("\u3000\u3000登记号：");
        JLabel structureLabel = new JLabel("结构图名称：");
        JLabel formulaLabel = new JLabel("\u3000\u3000\u3000公式：");
        JLabel priceLabel = new JLabel("\u3000\u3000\u3000价格：");
        JLabel realstockLabel = new JLabel("\u3000\u3000\u3000数量：");
        JLabel categoryLabel = new JLabel("\u3000\u3000\u3000类别：");


        productname = new JTextField(14);
        cas = new JTextField(14);
        structure = new JTextField(14);
        formula = new JTextField(14);
        price = new JTextField(14);
        realstock = new JTextField(14);
        category = new JTextField(14);

        productname.setText(product.getProductname());
        cas.setText(product.getCas());
        structure.setText(product.getStructure());
        formula.setText(product.getFormula());
        price.setText(product.getPrice());
        realstock.setText(product.getRealstock());
        category.setText(product.getCategory());


        JButton submitButton = new JButton("确定");
        JButton exitButton = new JButton("退出");

        addProductPanel.add(productnameLabel);
        addProductPanel.add(new JScrollPane(productname));
        addProductPanel.add(casLabel);
        addProductPanel.add(new JScrollPane(cas));
        addProductPanel.add(structureLabel);
        addProductPanel.add(new JScrollPane(structure));
        addProductPanel.add(formulaLabel);
        addProductPanel.add(new JScrollPane(formula));
        addProductPanel.add(priceLabel);
        addProductPanel.add(new JScrollPane(price));
        addProductPanel.add(realstockLabel);
        addProductPanel.add(new JScrollPane(realstock));
        addProductPanel.add(categoryLabel);
        addProductPanel.add(new JScrollPane(category));
        addProductPanel.add(submitButton);
        addProductPanel.add(exitButton);

        setResizable(false);
        setSize(260, 300);
        setLocation(300, 100);

        JPanel tipPanel = new JPanel();

        tip = new JLabel();

        tipPanel.add(tip);

        container.add(BorderLayout.CENTER, addProductPanel);
        container.add(BorderLayout.NORTH, tip);

        exitButton.addActionListener(new ExitActionListener());
        submitButton.addActionListener(new SubmitActionListener());

        this.addWindowListener(new UpdateProductFrame.WindowCloser());
        this.addWindowFocusListener(new WindowFocusListener() {// 设置父窗口
            public void windowGainedFocus(WindowEvent e) {
            }
            public void windowLostFocus(WindowEvent e) {
                e.getWindow().toFront();
            }
        });
        productname.setEnabled(false);
        try {
            productDataClient = new ProductDataClient();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    /**
     * 退出按钮事件监听
     * @author ascent
     */
    class ExitActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            setVisible(false);
            dispose();
        }
    }

    /**
     * 提交按钮事件监听
     * @author ascent
     */
    class SubmitActionListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            Product newProduct = new Product(productname.getText(),cas.getText(),structure.getText(),formula.getText(),price.getText(),realstock.getText(),category.getText());
            Boolean bo = productDataClient.updateProduct(newProduct);
            if (bo) {
                tip.setText("修改成功！");
                categoryText = category.getText();
            } else {
                tip.setText("修改失败");
            }
        }
    }

    /**
     * "关闭窗口"事件处理内部类
     * @author ascent
     */
    class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            dispose();
        }
    }
}
